package com.wmlt.dao.impl;

import com.wmlt.dao.OrderDao;
import com.wmlt.entity.Article;
import com.wmlt.entity.Order;
import com.wmlt.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class OrderDaoImpl implements OrderDao {
    String sql = "";
    PreparedStatement prea = null;
    ResultSet res = null;
    Article article = null;
    int result = 0;
    Connection connection = null;

    @Override
    public int inserOrder(Order order) {
        connection = JdbcUtil.getConnection();
        try {
            sql = "insert into `order`(user_id,order_id,order_name,order_money,order_message,order_status) values (?,?,?,?,?,?);";
            prea = connection.prepareStatement(sql);
            prea.setString(1, order.getUserId());
            prea.setString(2, order.getOrderId());
            prea.setString(3, order.getOrderName());
            prea.setString(4, order.getOrderMonry());
            prea.setString(5, order.getOrderMessage());
            prea.setInt(6, 1);
            return prea.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            JdbcUtil.close(null, prea, null);
        }
        return result;
    }

    @Override
    public int queryOrderStatus(String orderId) {
        connection = JdbcUtil.getConnection();
        try {
            sql = "select order_status from `order` where order_id=?";
            prea = connection.prepareStatement(sql);
            prea.setString(1, orderId);
            res=prea.executeQuery();
            while (res.next()){
                result=res.getInt(1);
            }
            System.out.println(result);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            JdbcUtil.close(null, prea, connection);
        }
        return result;
    }
}